﻿#region Using

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using XNgine.Services;

#endregion

namespace XNgine.Input
{
    /// <summary>
    /// States of a button (mouse, keyboard, gamepad,... any button)
    /// </summary>
    public enum PressedState
    {
        Pressed,
        Released,
        Held,
        Idle
    }

    /// <summary>
    /// The base input device class. Other input device types will
    /// inherit from it.
    /// </summary>
    /// <typeparam name="T">Type of the State that the device is providing</typeparam>
    public abstract class InputDevice<T> : GameComponent, IInputDevice<T>
    {
        protected InputDevice() : base(XEngine.Instance)
        {
        }

        /// <summary>
        /// Gets the current state of input device
        /// </summary>
        public abstract T State { get; }

        public abstract void Update();

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            Update();
        }

        protected static PressedState CheckPressedState(ButtonState currentState, PressedState lastState)
        {
            if (currentState == ButtonState.Pressed)
            {
                if (lastState == PressedState.Pressed || lastState == PressedState.Held)
                {
                    return PressedState.Held;
                }
                return PressedState.Pressed;
            }

            if (lastState != PressedState.Released && lastState != PressedState.Idle)
            {
                return PressedState.Released;
            }

            return PressedState.Idle;
        }
    }
}